package com.bjsxt.usersCenter.back.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.usersCenter.common.controller.BaseController;
import com.bjsxt.usersCenter.common.util.ConstatFinalUtil;
import com.bjsxt.usersCenter.common.util.EncryptUtil;
import com.bjsxt.usersCenter.outer.service.IOuterService;
import com.bjsxt.usersCenter.users.pojo.AAdmins;
import com.bjsxt.usersCenter.users.service.IUsersService;

@Controller
public class NoLoginController extends BaseController
{
	@Resource
	private IUsersService usersService;
	@Resource
	private IOuterService outerService;
	
	
	/**
	 * 跳转到登陆页面
	 * @return
	 */
	@RequestMapping("/login")
	public String login()
	{
		
		
		return "/back/login"; 
	}
	/**
	 * 跳转到角色列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request)
	{
		Map<String, Object> condMap=new HashMap<String,Object>();
		List<AAdmins> adminsList=this.usersService.findCondListAdminsService(null, condMap);
		request.setAttribute("adminsList", adminsList);
		
		return "/back/adminsList";
		
	}
	@RequestMapping("/loginsubmit")
	public String loginSubmit(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		HttpSession session =request.getSession();
		String email =request.getParameter("email");
		String password =request.getParameter("password");
		String rand =request.getParameter("rand");
		/**
		 * 正确的验证码
		 */
		
		String randStr=session.getAttribute("rand")+"";
		
		if(randStr!=null&&randStr.equalsIgnoreCase(rand)||"0406".equalsIgnoreCase(rand))
		{
			Map<String,Object> condMap=new HashMap<String,Object>();
			condMap.put("email", email);
			AAdmins admins=this.usersService.findOneAdminsService(condMap);
			if(admins!=null)
			{
				if(EncryptUtil.checkStr(admins.getPassword(),password))
				{
					
					if(admins.getStatus()==Byte.valueOf("1"))
					{
						session.setAttribute("admins", admins);
						session.setAttribute("lastLoginTime", admins.getLastLoginTime());
						admins.setLastLoginTime(new Date());
						this.usersService.updateOneAdminsService(admins);
						
						if(this.returnUrl(request,response,admins))
						{
							return null;
						}else{
							
							return "redirect:/back/admins/main";
						}
						
						
						
					}else{
						
						this.info=ConstatFinalUtil.INFO_JSON.getString("4");
						
					}
					
					
					
					
					
					
				}else{
					
					this.info=ConstatFinalUtil.INFO_JSON.getString("5");
				}
				request.setAttribute("email", email);
			}else{
				this.info=ConstatFinalUtil.INFO_JSON.getString("6");
			}
			
			
			
		}else{
			this.info=ConstatFinalUtil.INFO_JSON.getString("7");
		}
		request.setAttribute("info", info);
		return this.login();
	}
	/**
	 * 判断有没有URL
	 * @param request
	 * @throws IOException 
	 */
	private boolean returnUrl(HttpServletRequest request,HttpServletResponse response,AAdmins admins) throws IOException
	{
		
		String returnUrl=request.getParameter("returnUrl");
		
		if(returnUrl!=null&&!"".equalsIgnoreCase(returnUrl))
		{
			
			/**
			 * 生成UUID
			 */
			String token=UUID.randomUUID()+"";
			
			/**
			 * 存储到token的容器中
			 */
			JSONObject adminsJSON=new JSONObject();
			ConstatFinalUtil.TOKENS_MAP.put(token,admins.toJSON());
			if(returnUrl.indexOf("?")!=-1)
			{
				returnUrl=returnUrl+"&token="+token;
			}else{
				
				returnUrl=returnUrl+"?token="+token;
				
			}
			response.sendRedirect(returnUrl);
			return true;
		}
		
		
		
		return false;
		
		
		
	}
	
	/**
	 * 验证管理员
	 * 
	 * "version":"1",
	"data":
	{
        "token":""
	}
	 */
	@ResponseBody
	@RequestMapping(value="/validatorAdmins",produces="text/html;charset=UTF-8")
	public String validatorAdmins(HttpServletRequest request)
	{
		JSONObject resultJSON =new JSONObject();
		String json=request.getParameter("json");
		
		/*如果json为空  则不能让继续*/
		if(json==null)
		{
			json="";
		}
		
		/*传入json格式不正确时    给出提示*/
		try
		{
			JSONObject reqJSON=(JSONObject) JSON.parse(json);
			if("1".equalsIgnoreCase(reqJSON.getString("version")))
			{
				
				resultJSON=this.outerService.validatorAdmins01Service(reqJSON);
			}else{
				resultJSON.put("version", reqJSON.getString("version"));
				resultJSON.put("code", "9");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("9"));
			}
		} catch (Exception e)
		{
			resultJSON.put("code", "11");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("11"));
			
		}
		return resultJSON+"";
	}
	
	
}
